/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

// Created on 24.07.2004 by RST.

// $Id: QuakeFile.java,v 1.6 2005/11/20 22:18:34 salomo Exp $
using System;
using Suake2.UI.game;
using Com = Suake2.UI.qcommon.Com;
namespace Suake2.UI.util
{
	
	/// <summary> RandomAccessFile, but handles readString/WriteString specially and offers
	/// other helper functions
	/// </summary>
	//UPGRADE_TODO: Class 'java.io.RandomAccessFile' was converted to 'System.IO.FileStream' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioRandomAccessFile'"
	public class QuakeFile:System.IO.FileStream
	{
		
		/// <summary>Standard Constructor. </summary>
		//UPGRADE_ISSUE: Constructor 'java.io.RandomAccessFile.RandomAccessFile' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioRandomAccessFileRandomAccessFile_javalangString_javalangString'"
        // HZ: modifique el constructor, cambie y puse FileMode
		public QuakeFile(System.String filename, System.IO.FileMode mode):base(filename, mode)
		{
		}
		
		/// <summary>Writes a Vector to a RandomAccessFile. </summary>
		public virtual void  writeVector(float[] v)
		{
			for (int n = 0; n < 3; n++)
			{
				System.IO.BinaryWriter temp_BinaryWriter;
				temp_BinaryWriter = new System.IO.BinaryWriter(this);
				temp_BinaryWriter.Write((System.Single) v[n]);
			}
		}
		
		/// <summary>Writes a Vector to a RandomAccessFile. </summary>
		public virtual float[] readVector()
		{
			float[] res = new float[]{0, 0, 0};
			for (int n = 0; n < 3; n++)
			{
				System.IO.BinaryReader temp_BinaryReader;
				temp_BinaryReader = new System.IO.BinaryReader(this);
				temp_BinaryReader.BaseStream.Position = Position;
				res[n] = temp_BinaryReader.ReadSingle();
			}
			
			return res;
		}
		
		/// <summary>Reads a length specified string from a file. </summary>
		public virtual System.String readString()
		{
			System.IO.BinaryReader temp_BinaryReader;
			temp_BinaryReader = new System.IO.BinaryReader(this);
			temp_BinaryReader.BaseStream.Position = Position;
			int len = temp_BinaryReader.ReadInt32();
			
			if (len == - 1)
				return null;
			
			if (len == 0)
				return "";
			
			sbyte[] bb = new sbyte[len];
			
			SupportClass.ReadInput((System.IO.FileStream) this, bb, 0, len);
			
			return new System.String(SupportClass.ToCharArray(bb), 0, len);
		}
		
		/// <summary>Writes a length specified string to a file. </summary>
		public virtual void  writeString(System.String s)
		{
			if (s == null)
			{
				System.IO.BinaryWriter temp_BinaryWriter;
				temp_BinaryWriter = new System.IO.BinaryWriter(this);
				temp_BinaryWriter.Write((System.Int32) (- 1));
				return ;
			}
			
			System.IO.BinaryWriter temp_BinaryWriter2;
			temp_BinaryWriter2 = new System.IO.BinaryWriter(this);
			temp_BinaryWriter2.Write((System.Int32) s.Length);
			if (s.Length != 0)
				SupportClass.RandomAccessFileSupport.WriteBytes(s, this);
		}
		
		/// <summary>Writes the edict reference. </summary>
		public virtual void  writeEdictRef(edict_t ent)
		{
			if (ent == null)
			{
				System.IO.BinaryWriter temp_BinaryWriter;
				temp_BinaryWriter = new System.IO.BinaryWriter(this);
				temp_BinaryWriter.Write((System.Int32) (- 1));
			}
			else
			{
				System.IO.BinaryWriter temp_BinaryWriter2;
				temp_BinaryWriter2 = new System.IO.BinaryWriter(this);
				temp_BinaryWriter2.Write((System.Int32) ent.s.number);
			}
		}
		
		/// <summary> Reads an edict index from a file and returns the edict.</summary>
		
		public virtual edict_t readEdictRef()
		{
			System.IO.BinaryReader temp_BinaryReader;
			temp_BinaryReader = new System.IO.BinaryReader(this);
			temp_BinaryReader.BaseStream.Position = Position;
			int i = temp_BinaryReader.ReadInt32();
			
			// handle -1
			if (i < 0)
				return null;
			
			if (i > GameBase.g_edicts.Length)
			{
				Com.DPrintf("jake2: illegal edict num:" + i + "\n");
				return null;
			}
			
			// valid edict.
			return GameBase.g_edicts[i];
		}
		
		/// <summary>Writes the Adapter-ID to the file. </summary>
		public virtual void  writeAdapter(SuperAdapter a)
		{
			System.IO.BinaryWriter temp_BinaryWriter;
			temp_BinaryWriter = new System.IO.BinaryWriter(this);
			temp_BinaryWriter.Write((System.Int32) 3988);
			if (a == null)
				writeString(null);
			else
			{
				System.String str = a.ID;
				if (a == null)
				{
					//UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
					Com.DPrintf("writeAdapter: invalid Adapter id for " + a + "\n");
				}
				writeString(str);
			}
		}
		
		/// <summary>Reads the adapter id and returns the adapter. </summary>
		public virtual SuperAdapter readAdapter()
		{
			System.IO.BinaryReader temp_BinaryReader;
			temp_BinaryReader = new System.IO.BinaryReader(this);
			temp_BinaryReader.BaseStream.Position = Position;
			if (temp_BinaryReader.ReadInt32() != 3988)
				Com.DPrintf("wrong read position: readadapter 3988 \n");
			
			System.String id = readString();
			
			if (id == null)
			{
				// null adapter. :-)
				return null;
			}
			
			return SuperAdapter.getFromID(id);
		}
		
		/// <summary>Writes an item reference. </summary>
		public virtual void  writeItem(gitem_t item)
		{
			if (item == null)
			{
				System.IO.BinaryWriter temp_BinaryWriter;
				temp_BinaryWriter = new System.IO.BinaryWriter(this);
				temp_BinaryWriter.Write((System.Int32) (- 1));
			}
			else
			{
				System.IO.BinaryWriter temp_BinaryWriter2;
				temp_BinaryWriter2 = new System.IO.BinaryWriter(this);
				temp_BinaryWriter2.Write((System.Int32) item.index);
			}
		}
		
		/// <summary>Reads the item index and returns the game item. </summary>
		public virtual gitem_t readItem()
		{
			System.IO.BinaryReader temp_BinaryReader;
			temp_BinaryReader = new System.IO.BinaryReader(this);
			temp_BinaryReader.BaseStream.Position = Position;
			int ndx = temp_BinaryReader.ReadInt32();
			if (ndx == - 1)
				return null;
			else
				return GameItemList.itemlist[ndx];
		}
	}
}